package com.qisu.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;


/**
 * 发送邮件工具
 *
 * @author lijin
 *
 */
@Component
@Configuration
public class ExceptionMail {
	private final Log log = LogFactory.get();
	private MailUtils mailSender;

	// @formatter:off
	@Value("${exception-log.enable:false}") private boolean enable;
	@Value("${exception-log.host:smtp.exmail.qq.com}") private String host;
	@Value("${exception-log.sender:lijin@idianniu.com}") private String sender;
	@Value("${exception-log.server-password:fUcS3QMVh8l0721D}") private String pwd;
	@Value("${exception-log.recipients:}") private String[] recipients;		// list of address seperated by comma (,)
	// @formatter:on

	public void send(String title, String body) {
		if(enable) {
			if(null == mailSender) {
				mailSender = new MailUtils(host, sender, pwd);
			}
			mailSender.sendMail(recipients, title, body);
			log.info("发送异常邮件");
		} else {
			log.warn("----------------------------------------------");
			log.warn("title=" + title + "\n");
			log.warn("body=" + body);
			log.warn("----------------------------------------------");
		}
	}
}